package com.atguigu.seckill.exception;


import com.atguigu.seckill.vo.CodeResponse;
import com.atguigu.seckill.vo.CodeResponseEnum;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;



@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public CodeResponse ExceptionHandler(Exception e){
        if(e instanceof  GlobalException){
            GlobalException ex=(GlobalException) e;
            return  CodeResponse.error(ex.getCodeResponseEnum());
        }else if(e instanceof BindException){
            BindException ex=(BindException) e;
            CodeResponse codeResponse=CodeResponse.error(CodeResponseEnum.BIND_ERROR);
            codeResponse.setMessage("参数校验异常"+ex.getBindingResult().getAllErrors().get(0).getDefaultMessage());
                return codeResponse;

        }
        return CodeResponse.error(CodeResponseEnum.ERROR);
    }

}
